﻿using CommunityToolkit.Mvvm.Input;
using HandyControl.Data;
using Koala.Pro.Helpers;
using Koala.Pro.Models;
using Koala.Pro.ViewModels;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Wpf.Ui.Controls;

namespace Koala.Pro.Views.Pages
{
    public partial class SymbolRegularSelectPage : Window, INavigableView<SymbolRegularListViewModel>
    {
        public SymbolRegularListViewModel ViewModel { get; }

        public SymbolRegularSelectPage(SymbolRegularListViewModel viewModel)
        {
            ViewModel = viewModel;
            base.DataContext = this;
            InitializeComponent();
        }

        private void SearchBar_OnSearchStarted(object sender, FunctionEventArgs<string> e)
        {
            ViewModel.Search();
        }

        [RelayCommand]
        private void Back()
        {
            Close();
        }

        [RelayCommand]
        private void Confirm()
        {
            if (ViewModel.SelectedItem != null)
            {
                base.DialogResult = true;
            }
            else
            {
                MessageDxUtil.ShowTips("请选择一条记录");
            }
        }

        private void RadioButton_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            RadioButton radioButton = (RadioButton)sender;
            if (radioButton != null && radioButton.Tag is CListItem selectedItem)
            {
                ViewModel.SelectedItem = selectedItem;
                base.DialogResult = true;
            }
        }

        private void RadioButton_Checked(object sender, RoutedEventArgs e)
        {
            RadioButton radioButton = (RadioButton)sender;
            if (radioButton != null && radioButton.IsChecked == true && radioButton.Tag is CListItem selectedItem)
            {
                ViewModel.SelectedItem = selectedItem;
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Controls.Button button = (System.Windows.Controls.Button)sender;
            if (button != null && button.Tag is CListItem selectedItem)
            {
                ViewModel.SelectedItem = selectedItem;
            }
        }

        private void Button_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            System.Windows.Controls.Button button = (System.Windows.Controls.Button)sender;
            if (button != null && button.Tag is CListItem selectedItem)
            {
                ViewModel.SelectedItem = selectedItem;
                base.DialogResult = true;
            }
        }
    }
}
